<template>
    <div>
        <div>
            <el-input placeholder="请输入角色英文名" style="width: 250px;margin-right: 8px" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input style="width: 250px;margin-right: 8px" placeholder="请输入角色中文名" v-model="role.namezh" @keydown.enter.native="addRole">
            </el-input>
            <el-button icon="el-icon-plus" type="primary" @click="addRole">添加</el-button>
        </div>
        <div style="margin-top: 10px;width: 350px">
            <el-collapse v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.namezh" :name="r.id" v-for="(r,index) in roles">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete" @click="deleteRole(r)"></el-button>
                        </div>
                        <div>
                            <el-tree show-checkbox
                                     node-key="id"
                                     ref="tree"
                                     :key="index"
                                     :default-checked-keys="selectedmenus"
                                     :data="allmenus"
                                     :props="defaultProps" ></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissManager",
        data(){
            return{
                role:{
                    name:'',
                    namezh:''
                },
                allmenus:[],
                selectedmenus:[],
                activeName:-1,
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                roles:[],
            }
        },
        mounted() {
            this.initRoles();
        },
        methods:{
            initRoles(){
                this.getRequest("/system/basic/permiss/").then(resp=>{
                    if (resp){
                        this.roles = resp;
                    }
                })
            },
            change(rid){
                if (rid){
                    if (this.allmenus != null){
                        this.initAllMenus();
                    }
                    this.initselectedmenus(rid);
                }
            },
            initAllMenus(){
                this.getRequest("/system/basic/permiss/menu").then(resp=>{
                    if (resp){
                        this.allmenus=resp;
                    }
                })
            },
            initselectedmenus(rid){
                this.getRequest("/system/basic/permiss/mids/"+rid).then(resp=>{
                    if (resp){
                        this.selectedmenus=[];
                        this.selectedmenus=resp;
                    }
                })
            },
            doUpdate(rid,index){
                let tree = this.$refs.tree[index];
                let checkedNodes = tree.getCheckedNodes(true);
                let url = '/system/basic/permiss/?rid='+rid;
                checkedNodes.forEach(key=>{
                    url+='&mids='+key.id;
                });
                this.putRequest(url).then(resp=>{
                    if (resp){
                        this.activeName=-1;
                    }
                })
            },
            cancelUpdate(){
                this.activeName=-1;
            },
            addRole(){
                if (this.role.name && this.role.namezh){
                    this.postRequest("/system/basic/permiss/role",this.role).then(resp=>{
                        if (resp){
                            this.initRoles();
                            this.role = {};
                        }
                    })
                } else {
                    this.$message.error('请输入完整！');
                }
            },
            deleteRole(role){
                    this.$confirm('此操作将永久删除【'+role.namezh+'】角色, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/system/basic/permiss/role/"+role.id).then(resp=>{
                            if (resp){
                                this.initRoles();
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消批量删除'
                        });
                    });
            }
        }

    }
</script>

<style scoped>

</style>